﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using Bitis_KeToanThue_Pro_Update.AccessDatabase;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Telerik.WinControls.Primitives;
using Telerik.WinControls.Layouts;
using Telerik.WinControls.Enumerations;
using Telerik.WinControls.Data;
using Bitis_KeToanThue_Pro_Update.Common;

namespace Bitis_KeToanThue_Pro_Update.Form
{
    public partial class frmMain : Telerik.WinControls.UI.RadForm
    {
        MainProgram mp = new MainProgram();
        public frmMain()
        {
            InitializeComponent();
          //  this.IsMdiContainer = true;
            taomenu(BienHeThong.ht_userid,BienHeThong.ht_MaNhomUser,BienHeThong.ht_MaChuongTrinhLoginSuseccFull,BienHeThong.ht_IDViTri,BienHeThong.ht_MaNhomMenuND, radMenu1);

        }


        private void taomenu(int UserID, int MaNhomUser, int MaChuongTrinh, int IDViTri, int MaNhomMenu, RadMenu menuname)
        {
            DataTable dt = new DataTable();
            dt = mp.loadmenushowtest(UserID,  MaNhomUser,  MaChuongTrinh,  IDViTri,  MaNhomMenu);

            //menu gồm 5 cấp
            if (dt.Rows.Count > 0)
            {


                menuname.BeginInit();
                radMenu1.Items.Clear();
                var menucap0 = from myRowmenucap0 in dt.AsEnumerable()
                               where myRowmenucap0.Field<int>("CapMenu") == 0
                               select myRowmenucap0;
                DataTable dtmenucap0 = new DataTable();
                try
                {
                    dtmenucap0 = menucap0.CopyToDataTable();
                }
                catch
                {
                    dtmenucap0.Clear();
                }
                //load menu cap 0 (menu root)
                if (dtmenucap0.Rows.Count > 0)
                {
                    for (int i = 0; i < dtmenucap0.Rows.Count; i++)
                    {
                        menuname.Items.Add(new RadMenuItem(dtmenucap0.Rows[i]["MenuName"].ToString()));
                        RadMenuItem radmenuitemcap1 = (RadMenuItem)menuname.Items[i];
                        radmenuitemcap1.Name = dtmenucap0.Rows[i]["ChuoiMenu"].ToString();
                        radmenuitemcap1.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap0.Rows[i]["Hinh"]);
                        //  radmenuitemcap1.BorderThickness = new System.Windows.Forms.Padding(10,3,10,3);
                        // radmenuitemcap1.BorderPrimitive.BoxStyle = BorderBoxStyle.SingleBorder;
                        radmenuitemcap1.BorderPrimitive.Width = 0;
                        radmenuitemcap1.BorderPrimitive.BottomWidth = 0;
                        radmenuitemcap1.BorderPrimitive.TopWidth = 0;
                        radmenuitemcap1.BorderPrimitive.LeftWidth = 0;
                        radmenuitemcap1.BorderPrimitive.RightWidth = 0;
                        radmenuitemcap1.Padding = new System.Windows.Forms.Padding(3, 3, 10, 3);

                        var menucap1 = from myRowmenucap1 in dt.AsEnumerable()
                                       where myRowmenucap1.Field<int>("CapMenu") == 1 && myRowmenucap1.Field<int>("MenuParent") == int.Parse(dtmenucap0.Rows[i]["ID"].ToString())
                                       select myRowmenucap1;
                        DataTable dtmenucap1 = new DataTable();

                        try
                        {
                            dtmenucap1 = menucap1.CopyToDataTable();
                        }
                        catch
                        {
                            dtmenucap1.Clear();
                        }
                        if (dtmenucap1.Rows.Count > 0)
                        {
                            RadMenuItem item = (RadMenuItem)menuname.Items[i];
                            for (int j = 0; j < dtmenucap1.Rows.Count; j++)
                            {
                                item.Items.Add(new RadMenuItem(dtmenucap1.Rows[j]["MenuName"].ToString()));
                                RadMenuItem radmenuitemcap2 = (RadMenuItem)item.Items[j];
                                radmenuitemcap2.Name = dtmenucap1.Rows[j]["ChuoiMenu"].ToString();
                                radmenuitemcap2.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap1.Rows[j]["Hinh"]);
                                var menucap2 = from myRowmenucap2 in dt.AsEnumerable()
                                               where myRowmenucap2.Field<int>("CapMenu") == 2 && myRowmenucap2.Field<int>("MenuParent") == int.Parse(dtmenucap1.Rows[j]["ID"].ToString())
                                               select myRowmenucap2;
                                DataTable dtmenucap2 = new DataTable();

                                try
                                {
                                    dtmenucap2 = menucap2.CopyToDataTable();
                                }
                                catch
                                {
                                    dtmenucap2.Clear();
                                }
                                if (dtmenucap2.Rows.Count > 0)
                                {
                                    RadMenuItem item2 = (RadMenuItem)item.Items[j];
                                    for (int k = 0; k < dtmenucap2.Rows.Count; k++)
                                    {
                                        item2.Items.Add(new RadMenuItem(dtmenucap2.Rows[k]["MenuName"].ToString()));
                                        RadMenuItem radmenuitemcap3 = (RadMenuItem)item2.Items[k];
                                        radmenuitemcap3.Name = dtmenucap2.Rows[k]["ChuoiMenu"].ToString();
                                        radmenuitemcap3.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap2.Rows[k]["Hinh"]);
                                        var menucap3 = from myRowmenucap3 in dt.AsEnumerable()
                                                       where myRowmenucap3.Field<int>("CapMenu") == 3 && myRowmenucap3.Field<int>("MenuParent") == int.Parse(dtmenucap2.Rows[k]["ID"].ToString())
                                                       select myRowmenucap3;
                                        DataTable dtmenucap3 = new DataTable();

                                        try
                                        {
                                            dtmenucap3 = menucap3.CopyToDataTable();
                                        }
                                        catch
                                        {
                                            dtmenucap3.Clear();
                                        }
                                        if (dtmenucap3.Rows.Count > 0)
                                        {
                                            RadMenuItem item3 = (RadMenuItem)item2.Items[k];
                                            for (int h = 0; h < dtmenucap3.Rows.Count; h++)
                                            {
                                                item3.Items.Add(new RadMenuItem(dtmenucap3.Rows[h]["MenuName"].ToString()));
                                                RadMenuItem radmenuitemcap4 = (RadMenuItem)item3.Items[h];
                                                radmenuitemcap4.Name = dtmenucap3.Rows[h]["ChuoiMenu"].ToString();

                                                radmenuitemcap4.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap3.Rows[h]["Hinh"]);

                                                var menucap4 = from myRowmenucap4 in dt.AsEnumerable()
                                                               where myRowmenucap4.Field<int>("CapMenu") == 4 && myRowmenucap4.Field<int>("MenuParent") == int.Parse(dtmenucap3.Rows[h]["ID"].ToString())
                                                               select myRowmenucap4;
                                                DataTable dtmenucap4 = new DataTable();

                                                try
                                                {
                                                    dtmenucap4 = menucap4.CopyToDataTable();
                                                }
                                                catch
                                                {
                                                    dtmenucap4.Clear();
                                                }
                                                if (dtmenucap4.Rows.Count > 0)
                                                {
                                                    RadMenuItem item4 = (RadMenuItem)item3.Items[h];
                                                    for (int n = 0; n < dtmenucap4.Rows.Count; n++)
                                                    {
                                                        item4.Items.Add(new RadMenuItem(dtmenucap4.Rows[n]["MenuName"].ToString()));
                                                        RadMenuItem radmenuitemcap5 = (RadMenuItem)item4.Items[n];
                                                        radmenuitemcap5.Name = dtmenucap4.Rows[n]["ChuoiMenu"].ToString();
                                                        radmenuitemcap5.Image = ClassXuLyChung.GetIamgeToDataBase(dtmenucap4.Rows[n]["Hinh"]);

                                                        ((RadMenuItem)item4.Items[n]).Click += new EventHandler(item_Click);

                                                    }
                                                }
                                                else
                                                {
                                                    ((RadMenuItem)item3.Items[h]).Click += new EventHandler(item_Click);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            ((RadMenuItem)item2.Items[k]).Click += new EventHandler(item_Click);
                                        }

                                    }

                                }
                                else
                                {
                                    ((RadMenuItem)item.Items[j]).Click += new EventHandler(item_Click);
                                }
                            }
                        }
                        else
                        {
                            ((RadMenuItem)menuname.Items[i]).Click += new EventHandler(item_Click);
                        }
                    }
                }

                menuname.EndInit();


            }

        }

        private void item_Click(object sender, EventArgs e)
        {
            string name = "";
            RadMenuItem item = ((RadMenuItem)sender);
            name = item.Name;
            BienHeThong.ht_chuoishowform = name.Substring(0, name.IndexOf("!!!!!"));
            BienHeThong.ht_moduldemenu = name.Substring(name.IndexOf("%%%%%") + 5, name.IndexOf("&&&&&") - name.IndexOf("%%%%%") - 5);
            BienHeThong.ht_tenform = name.Substring(name.IndexOf("!!!!!") + 5, name.IndexOf("%%%%%") - name.IndexOf("!!!!!") - 5);
            BienHeThong.ht_idphanquyen = ClassXuLyChung.GetSafeInt(name.Substring(name.IndexOf("&&&&&") + 5, name.IndexOf("^^^^^") - name.IndexOf("&&&&&") - 5));
            BienHeThong.ht_command = ClassXuLyChung.GetSafeInt(name.Substring(name.IndexOf("^^^^^") + 5, name.Length - name.IndexOf("^^^^^") - 5));
            DataTable dt = new DataTable();
            dt = mp.layquyen(BienHeThong.ht_idphanquyen);
            if (dt.Rows.Count > 0)
            {
                BienHeThong.ht_quyenxem = ClassXuLyChung.GetSafeBoolInt(dt.Rows[0]["Quyen_Xem"]);
                BienHeThong.ht_quyencapnhat = ClassXuLyChung.GetSafeBoolInt(dt.Rows[0]["Quyen_CapNhat"]);
                BienHeThong.ht_quyenxoa = ClassXuLyChung.GetSafeBoolInt(dt.Rows[0]["Quyen_Xoa"]);
            }
            else
            {
                BienHeThong.ht_quyenxem = 0;
                BienHeThong.ht_quyencapnhat = 0;
                BienHeThong.ht_quyenxoa = 0;
            }
            if (BienHeThong.ht_tenform == "frmManHinhChinh")
            {
                ShowFormMenuMainProject.openformcloseformfinal("Bitis_KeToanThue_Pro_Update.Form.frmManHinhChinh", "frmManHinhChinh", this);
            }
            else
            {
                if (BienHeThong.ht_tenform == "Thoát")
                {
                    this.Close();
                    
                }
                else
                {
                    if (BienHeThong.ht_tenform == "frmLogin")
                    {
                        frmLogin frmdangnhap = new frmLogin();
                        frmdangnhap.DanhNhapThanhCong += new DangNhapThanhCongHandler(frm_DangNhapThanhCong);
                        frmdangnhap.ShowDialog();
                    }
                    else
                    {
                        switch (BienHeThong.ht_moduldemenu)
                        {
                            case "Bitis_KeToanThue_Pro_Update":
                                if (BienHeThong.ht_command == 1 || BienHeThong.ht_command == 2)
                                {
                                    Bitis_KeToanThue_Pro_Update.Common.ShowFormMenuMainProject.showformbymenuloaddulieudatabase(BienHeThong.ht_tenform, BienHeThong.ht_chuoishowform,BienHeThong.ht_command);
                                }
                                else
                                {
                                   
                                        if (BienHeThong.ht_command == 3)
                                        {
                                            Bitis_KeToanThue_Pro_Update.Common.ShowFormMenuMainProject.openformcloseformfinal(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, this);
                                            
                                        }
                                        else
                                        {
                                            if (BienHeThong.ht_command == 4)
                                            {
                                                this.Close();
                                            }
                                            else
                                            {
                                                Bitis_KeToanThue_Pro_Update.Common.ShowFormMenuMainProject.MdiShowForm(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, radPageView1, Bitis_KeToanThue_Pro_Update.Properties.Resources.Counter_icon);
                                            }
                                        }
                                    
                                }
                                break;
                            case "Bitis_KeToanThue_Pro":
                                if (BienHeThong.ht_command == 1 || BienHeThong.ht_command == 2)
                                {
                                    Bitis_KeToanThue_Pro.Common.ShowFormMenuKeToanThueProject.showformbymenuloaddulieudatabase(BienHeThong.ht_tenform, BienHeThong.ht_chuoishowform, BienHeThong.ht_command);
                                }
                                else
                                {

                                    if (BienHeThong.ht_command == 3)
                                    {
                                        Bitis_KeToanThue_Pro.Common.ShowFormMenuKeToanThueProject.openformcloseformfinal(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, this);

                                    }
                                    else
                                    {
                                        if (BienHeThong.ht_command == 4)
                                        {
                                            this.Close();
                                        }
                                        else
                                        {
                                            Bitis_KeToanThue_Pro.Common.ShowFormMenuKeToanThueProject.MdiShowForm(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, radPageView1, Bitis_KeToanThue_Pro_Update.Properties.Resources.Counter_icon);
                                        }
                                    }

                                }
                                break;
                            case "Bitis_KeToanThue_Pro_QuanTri":
                                if (BienHeThong.ht_command == 1 || BienHeThong.ht_command == 2)
                                {
                                    Bitis_KeToanThue_Pro_QuanTri.Common.ShowFormMenuQuanTriProject.showformbymenuloaddulieudatabase(BienHeThong.ht_tenform, BienHeThong.ht_chuoishowform, BienHeThong.ht_command);
                                }
                                else
                                {

                                    if (BienHeThong.ht_command == 3)
                                    {
                                        Bitis_KeToanThue_Pro_QuanTri.Common.ShowFormMenuQuanTriProject.openformcloseformfinal(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, this);

                                    }
                                    else
                                    {
                                        if (BienHeThong.ht_command == 4)
                                        {
                                            this.Close();
                                        }
                                        else
                                        {
                                            Bitis_KeToanThue_Pro_QuanTri.Common.ShowFormMenuQuanTriProject.MdiShowForm(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, radPageView1, Bitis_KeToanThue_Pro_Update.Properties.Resources.con_address);
                                        }
                                    }

                                }
                                break;
                            case "Bitis_KeToanThue_Pro_DanhMuc":
                                if (BienHeThong.ht_command == 1 || BienHeThong.ht_command == 2)
                                {
                                    Bitis_KeToanThue_Pro_DanhMuc.Common.ShowFormMenuDanhMucProject.showformbymenuloaddulieudatabase(BienHeThong.ht_tenform, BienHeThong.ht_chuoishowform, BienHeThong.ht_command);
                                }
                                else
                                {

                                    if (BienHeThong.ht_command == 3)
                                    {
                                        Bitis_KeToanThue_Pro_DanhMuc.Common.ShowFormMenuDanhMucProject.openformcloseformfinal(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, this);

                                    }
                                    else
                                    {
                                        if (BienHeThong.ht_command == 4)
                                        {
                                            this.Close();
                                        }
                                        else
                                        {
                                            Bitis_KeToanThue_Pro_DanhMuc.Common.ShowFormMenuDanhMucProject.MdiShowForm(BienHeThong.ht_chuoishowform, BienHeThong.ht_tenform, radPageView1, Bitis_KeToanThue_Pro_Update.Properties.Resources.con_address);
                                        }
                                    }

                                }
                                break;
                        }
                    }
                }
            }


        }
        private void frm_DangNhapThanhCong(object sender, DangNhapThanhCongEventArgs e)
        {

            loadlablestrip(e.dl_manv, e.dl_tennv, e.dl_TenPB, e.dl_ChucVu, String.Format("{0:MM/dd/yyyy}", DateTime.Now));
            taomenu(e.dl_userid, e.dl_MaNhomUser, e.dl_MaChuongTrinhLoginSuseccFull, e.dl_IDViTri, e.dl_MaNhomMenuND, radMenu1);
        }

        private void loadlablestrip(string manv, string tennv, string phongban, string chucvu, string ngaylamviec)
        {
            buttonMaNV.Text = manv;
            buttonTenNV.Text = tennv;
            buttonPhongBan.Text = phongban;
            buttonChucVu.Text = chucvu;
            buttonNgayLamViec.Text = ngaylamviec;
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            loadlablestrip(BienHeThong.ht_manv, BienHeThong.ht_tennv, BienHeThong.ht_TenPB, BienHeThong.ht_ChucVu, String.Format("{0:MM/dd/yyyy}", DateTime.Now));
        }

      
        private void kiemtradongform(RadPageView pageview)
        {
            foreach (RadPageViewPage paget in pageview.Pages)
            {
                if (paget.Controls.Count == 0)
                {
                    pageview.Pages.Remove(paget);
                }

            }
        }
        private void btnKiemTraDongForm_Click(object sender, EventArgs e)
        {
            kiemtradongform(radPageView1);
           
        }

        
      

       

      

        
    }
}
